package org.xqyy.eval.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.xqyy.eval.dao.DeptMapper;
import org.xqyy.eval.dao.IDeptDao;
import org.xqyy.eval.dao.TDeptMapper;
import org.xqyy.eval.model.Dept;
import org.xqyy.eval.model.TDept;
import org.xqyy.eval.service.IDeptService;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by luoxiaoming on 16-12-24.
 */

@Component
@Transactional
public class DeptService implements IDeptService {

    @Resource
    private IDeptDao deptDao;
    @Resource
    private DeptMapper deptMapper;

    @Autowired
    private TDeptMapper tDeptMapper;

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, readOnly = false)
    public void add(Map<String, Object> params) {
        deptDao.add(params);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, readOnly = false)
    public void modify(Map<String, Object> params) {
        deptDao.modify(params);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, readOnly = false)
    public void delete(Map<String, Object> params) {
        deptDao.delete(params);
    }

    @Override
    public Map<String, Object> loadOne(String deptCode) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("code", deptCode);
        Map<String, Object> data = deptDao.loadOne(params);
        return data;
    }

    @Override
    public List<Map<String, Object>> load(Map<String, Object> params) {
        return deptDao.load(params);
    }

    @Override
    public List<TDept> selectAllDept() {

        Example example = new Example(TDept.class);
        example.createCriteria().andNotEqualTo("pcode","").andEqualTo("status",1);;
        return tDeptMapper.selectByExample(example);
    }

    public List<TDept> selectDeptByGroupId(String groupCode){
        Example example = new Example(TDept.class);
        example.createCriteria()
                .andEqualTo("groupCode",groupCode)
                .andEqualTo("status",1);
        return tDeptMapper.selectByExample(example);
    }

    @Override
    public List<TDept> selectAllTDept() {
        return tDeptMapper.selectAllTDept();
    }

    /**
     * 添加科室
     *
     * @param tDept
     * @return
     */
    @Override
    public Boolean addTDept(TDept tDept) {
        tDept.setId(null);
        tDept.setStatus("1");
        tDept.setPcode("1");
        tDeptMapper.insert(tDept);
        return true;
    }

    /**
     * 修改科室
     *
     * @param tDept
     * @return
     */
    @Override
    public Boolean updateTDept(TDept tDept) {
        TDept tempdept =  findTDeptById(tDept);
        tDept.setPcode(tempdept.getPcode());
        tDept.setStatus(tempdept.getStatus());
        tDeptMapper.updateByPrimaryKey(tDept);
        return true;
    }

    /**
     * 根据ID查询科室
     *
     * @param tDept
     * @return
     */
    @Override
    public TDept findTDeptById(TDept tDept) {
        return tDeptMapper.selectByPrimaryKey(tDept.getId());
    }

    /**
     * 转换科室状态
     * @param tDept
     * @return
     */
    @Override
    public Boolean convertTDept(TDept tDept) {
        tDept = tDeptMapper.selectByPrimaryKey(tDept.getId());
        if ("1".equals(tDept.getStatus()) ){
            tDept.setStatus("0");
        }else{
            tDept.setStatus("1");
        }
        tDeptMapper.updateByPrimaryKey(tDept);
        return true;
    }

    /**
     * 查询所有已启用的科室
     * @return
     */
    public  List<TDept> selectAllTDeptIsEnable(){
        return tDeptMapper.selectAllTDeptIsEnable();
    }
}
